<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="">
        <el-breadcrumb-item
          ><div class="div1">基本配置</div></el-breadcrumb-item
        >
        <el-breadcrumb-item
          ><div class="div2">轮播配置</div></el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <!-- 面包屑导航 -->
    <div class="content">
      <p class="header">查询客户</p>
      <el-form
        :inline="true"
        ref="form"
        :model="queryForm"
        label-width=""
        size="mini"
      >
        <el-form-item label="类型">
          <el-select v-model="queryForm.type" placeholder="请选择类型">
            <el-option
              v-for="items in status"
              :key="items.dictId"
              :label="items.dictName"
              :value="items.dictId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-button size="small" type="primary" @click="imglist(1)"
          >搜索</el-button
        >
        <el-button size="small" type="primary" @click="dialogFormVisible = true"
          >添加轮播</el-button
        >
      </el-form>
    </div>
    <div class="table">
      <el-table :data="tableData.list" style="width: 100%">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="图片">
          <template slot-scope="scope">
            <img :src="ImgHttps + scope.row.image" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="imageName" label="图片名称"></el-table-column>
        <el-table-column prop="comment" label="备注"></el-table-column>
        <el-table-column prop="typeName" label="类型"></el-table-column>
        <el-table-column prop="createdTime" label="上传时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <p class="operation" @click="fixEvent(scope.row)">修改</p>
            <!-- 状态（0:启用，1:冻结） -->
            <p
              class="operation"
              v-if="scope.row.status == 0"
              @click="startStop(scope.row, 1)"
            >
              禁用
            </p>
            <p
              class="operation"
              v-if="scope.row.status == 1"
              @click="startStop(scope.row, 0)"
            >
              启动
            </p>
            <p class="operation" @click="delectImg(scope.row)">删除</p>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页start -->
      <div class="page">
        <div class="block">
          <span class="demonstration">共{{ tableData.totalPage }}页</span>
          <span class="demonstration">共{{ tableData.totalNum }}条</span>
          <el-pagination
            background=""
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage1"
            :page-size="1"
            layout="prev, pager, next"
            prev-text="上一页"
            next-text="下一页"
            :total="tableData.totalPage"
          >
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
      title="设置轮播"
    >
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width=""
        size="mini"
      >
        <el-form-item label="轮播名称">
          <el-input v-model="form.name" placeholder="请输入轮播名称"></el-input>
        </el-form-item>

        <el-form-item label="轮播排序">
          <el-input
            v-model="form.identifier"
            placeholder="请输入序号"
          ></el-input>
        </el-form-item>

        <el-form-item label="类型">
          <el-select v-model="form.btype" placeholder="请选择类型">
            <el-option
              v-for="items in status"
              :key="items.dictId"
              :label="items.dictName"
              :value="items.dictId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- <el-form-item label="备注"> -->
      <span>备注</span>
      <el-input
        type="textarea"
        placeholder="请输入内容"
        v-model="form.textarea"
      ></el-input>
      <!-- </el-form-item> -->
      <el-upload
        class="upload-demo"
        ref="upload"
        list-type="picture"
        action="#"
        :show-file-list="false"
        :auto-upload="true"
        :file-list="fileList"
        :http-request="IndexImport"
        :limit="1"
      >
        <el-button slot="trigger" size="small" type="primary"
          >点击上传</el-button
        >
      </el-upload>
      <div class="notes">
        <p v-for="(item, index) in fileList" :key="index">
          <img v-if="!is_null(fileList)" :src="item.img" class="avatar" />
          <span v-if="!is_null(fileList)" @click="close(index)">×</span>
        </p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          size="small"
          @click="
            dialogFormVisible = false;
            uploadTrue();
          "
          >确 定</el-button
        >
        <el-button size="small" @click="dialogFormVisible = false"
          >取 消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fileList: [],
      imageUrl: [],
      tableData: [],
      status: [],
      queryForm: {
        type: "",
      },
      form: {
        btype: "0",
        identifier: "0",
        name: "",
        textarea: "",
      },
      id: "",
      dialogFormVisible: false,
      currentPage1: 1,
    };
  },
  created() {
    this.typeAxios();
    this.imglist(1);
  },
  activated() {
    // this.typeAxios();
    // this.imglist(1);
  },
  methods: {
    // 类型请求
    typeAxios() {
      this.axios
        .get("/sys/v1/common/by/group?groupId=" + "carousel_type", {
          header: {
            isLoading: false,
          },
        })
        .then((res) => {
          if (res.code == 0) {
            this.status = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 确定上传
    uploadTrue() {
      this.axios
        .post("/sys/v1/sys/index/save", {
          id: this.id,
          comment: this.form.textarea,
          image: this.imageUrl,
          imageName: this.form.name,
          sort: this.form.identifier,
          status: 0,
          type: this.form.btype,
        })
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            this.$message.success("上传成功");
            this.fileList = [];
            this.id = "";
            this.imglist("1");
          } else {
            this.$message.error(res.message);
          }
        });
    },
    handleCurrentChange(val) {
      this.imglist(`${val}`);
    },
    // 修改
    fixEvent(row) {
      this.dialogFormVisible = true;
      this.axios
        .get("/sys/v1/sys/index/detail?id=" + row.id, {
          headers: {
            isLoading: false,
          },
        })
        .then((res) => {
          if (res.code == 0) {
            this.form = {
              btype: res.data.type,
              identifier: res.data.sort,
              name: res.data.imageName,
              textarea: res.data.comment,
            };
            this.id = res.data.id;
            let obj = {
              img: this.ImgHttps + res.data.image,
            };
            this.imageUrl = res.data.image;
            this.fileList.push(obj);
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 图片上传
    IndexImport(fileObj) {
      let formData = new FormData();
      formData.append("file", fileObj.file);
      let config = {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      };
      this.axios
        .post(
          "/sys/v1/common/upload/files?pathName=internalSystem",
          formData,
          config
        )
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            this.imageUrl = res.data.url;
            let obj = {
              img: this.ImgHttps + res.data.url,
            };
            this.fileList.push(obj);
            // this.imageUrl.push(res.data.url)
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 删除图片
    close(index) {
      this.fileList.splice(index, 1);
      this.imageUrl = "";
    },

    imglist(pageNo) {
      this.axios
        .post("/sys/v1/sys/index/list", {
          pageNo: pageNo,
          pageSize: 8,
          type: this.queryForm.type,
          status: 0,
        })
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            this.tableData = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 上传页面
    delectImg(row) {
      let array = [];
      array.push(row.id);
      this.$confirm("确定要删除该数据吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .get("/sys/v1/sys/index/delete?ids=" + array, {
              headers: {
                isLoading: false,
              },
            })
            .then((res) => {
              if (res.code == 0) {
                this.$message.success("删除成功");
                this.imglist("1");
              } else {
                this.$message.error(res.message);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    startStop(row, status) {
      let array = [];
      array.push(row.id);
      this.axios
        .post(
          "/sys/v1/sys/index/updatestatus",
          {
            ids: array,
            status: status,
          },
          {
            headers: {
              isLoading: false,
            },
          }
        )
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            this.$message.success("修改成功");
            this.imglist("1");
          } else {
            this.$message.error(res.message);
          }
        });
    },
    is_null(content) {
      if (content) {
        return false;
      } else {
        return true;
      }
    },
  },
};
</script>

<style scoped lang="scss">
.div1 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb2.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}
.div2 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb1.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #fff;
}
.content {
  background: #fff;
  padding: 10px;
}
/deep/.el-textarea {
  width: 90%;
  display: inline-block;
  margin-left: 10px;
  vertical-align: middle;
}
.upload-demo {
  display: inline-block;
  margin-right: 15px;
  margin-top: 10px;
}
.notes {
  p {
    margin-top: 10px;
    display: inline-block;
    margin-right: 10px;
    position: relative;
    img {
      width: 120px;
      height: 120px;
    }
  }
  span {
    display: block;
    width: 18px;
    height: 18px;
    line-height: 15px;
    text-align: center;
    font-size: 13px;
    color: #fff;
    background: #409eff;
    position: absolute;
    top: -5px;
    right: -6px;
    border-radius: 50%;
  }
}
/deep/.el-dialog__header {
  background: #409eff;
  padding: 10px 20px 10px;
  .el-dialog__title {
    color: #fff;
  }
}
/deep/.el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}
/deep/.el-dialog {
  position: relative;
  margin: 0 auto 50px;
  border-radius: 2px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
}
/deep/.el-dialog__headerbtn {
  top: 15px;
}
.table {
  /deep/.el-table td,
  .el-table th {
    text-align: center;
  }
  /deep/ .el-table th > .cell {
    text-align: center;
    color: rgb(45, 125, 223);
    font-size: 15px;
  }
  /deep/.el-table__row:hover {
    color: rgb(45, 125, 223);
  }
  img {
    width: 60px;
    height: 60px;
  }
}
.operation {
  display: inline-block;
  vertical-align: middle;
  // margin-right:20px;
  padding: 0 5px;
  img {
    width: 25px;
    height: 25px;
  }
}
.operation:hover {
  cursor: pointer;
}
.header {
  font-size: 16px;
  color: #2d7edf;
  padding-bottom: 10px;
}
/deep/.el-input {
  // width:200px;
}
/deep/.el-input__inner {
  // width:200px;
  height: 32px;
  line-height: 32px;
}
.page {
  margin: 20px 0;
  text-align: right;
  width: 100%;
  span {
    font-size: 15px;
    color: #666;
    display: inline-block;
    margin-left: 10px;
    vertical-align: middle;
  }
  .el-pagination {
    display: inline-block;
    vertical-align: middle;
  }
}
</style>
